package event

import (
	models2 "beergo/app/admin/models"
	"beergo/app/sys/models"
	"beergo/lib/event"
	"beergo/lib/tool"
	"github.com/beego/beego/v2/server/web/context"
)

func init() {
	OnLogin()
}
func OnLogin()  {
	event.ListenJoin("Login", func(v interface{}, ctx *context.Context) {
		account,ok := v.(map[string]interface{})["account"]
		if ok {
			user,ok :=account.(models.User)
			if ok {
				repAdmin := new(models2.AdminUserRepository)
				admin:=repAdmin.FindOneBy(tool.StringKeyMap{"uid": user.Id},nil)
				if admin.Id == 0 {
					//创建一个账号
					admin.Name = user.Nickname
					admin.Salt = user.Salt
					admin.Uid = user.Id
				}
				admin.Token = user.Token
				admin.SetEventOff()
				_,err:=admin.Save()
				if err != nil {
					v.(map[string]interface{})["admin"] = err
					return
				}
				v.(map[string]interface{})["admin"] = admin.ToArrayEdit()
			}
		}
	})
}

func OnCreate() {

}